<?php

namespace App\Model;


use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use mysql_xdevapi\Exception;

class SendGoodsRecord extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'send_goods_record';
    private $fields = ['id','user_id','uuid','goods_id','goods_name','sku_desc','desc','created_at','type','nickname','sys_notice'];
    protected $fillable = ['user_id','uuid','goods_id','goods_name','desc','sku_desc','type','nickname','sys_notice'];
    
    public function getList(Request $request){
        $limit = $request->input('limit',10);
        $page = $request->input('page',1);
        $uuid = $request->input('uuid','');
        $nickname = $request->input('nickname','');
        $type = $request->input('type','');
        $begin = $request->input('begin','');
        $end = $request->input('end','');
        
        $query = new SendGoodsRecord();
        
        if($uuid) $query = $query->where('uuid',$uuid);
        if($nickname) $query = $query->where('nickname','like',"%{$nickname}%");
        if($begin && !$end) $query = $query->where('created_at','>=', $begin);
        if(!$begin && $end) $query = $query->where('created_at','<=', $end);
        if($begin && $end) $query = $query->whereBetween('created_at',[$begin,$end]);
        if($type) $query = $query->where('type', $type);
        
        return $query->orderBy('id','desc')->paginate($limit,$this->fields,'page',$page);
    }
    
    public function createData(Request $request, array $fillData){
        return self::create(array_merge($request->all(),$fillData));
    }
    
    public function numbers(){
        return $this->belongsTo(Number::class,'user_id','user_id');
    }
}
